package com.yc.forum.servlet;

import com.yc.forum.dao.RedisDao;
import com.yc.forum.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * 用户给评论点赞的动作：
 *  1.要修改当前评论的点赞数量
 *  2.将该评论id存入被该用户点赞集合
 *  3.增加评论发布者1点积分
 */
@WebServlet(name = "EnterLikeReplyServlet",value = "/EnterLikeReplyServlet.s")
public class EnterLikeReplyServlet extends HttpServlet {
    RedisDao redisDao =new RedisDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session= req.getSession();
        String account;
        try{
            account=session.getAttribute("loginedUser").toString();
            String replyid=req.getParameter("replyid");
            //使该评论点赞数量加一
            Map<String,String> map=redisDao.queryReplySomeSumById(replyid);
            if(map.get("likesum")==null){//如果为空则为一，如果不为空则加一
                map.put("likesum",""+1);
            }else{
                map.put("likesum",""+(Integer.parseInt(map.get("likesum"))+1));
            }
            redisDao.updateReplySomeSum(replyid,map);

            //存入当前点赞账号，不可重复点赞
            redisDao.updateReplySuppAccount(replyid,account);

            //为评论的发布者增加1点积分
            String postAccount=req.getParameter("applyAccount");
            System.out.println("----------------init EnterLike---------------------------------");
            System.out.println(postAccount);
            Utils.addReward(postAccount,1);
        }catch(Exception e){
            Utils.toJson(-1,resp);
        }

    }
}
